package com.online.shopping.controller;

import com.online.shopping.pojo.CommonResult;
import com.online.shopping.pojo.TbTypeTemplate;
import com.online.shopping.service.TemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Rui
 * @version 1.0
 * @date 2020/11/15 13:20
 */
@Api(tags = "模板管理", description = "模板增删改查")
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/manager/template")
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    @ApiOperation("查询所有模板")
    @GetMapping("/findAllTemps")
    public List<TbTypeTemplate> findAllTemps(){
        List<TbTypeTemplate> allTemps = templateService.findAllTemps();
        log.info(String.valueOf(allTemps));
        return allTemps;
    }

    @ApiOperation("根据id查询模板")
    @GetMapping("/findTempById/{id}")
    public TbTypeTemplate findTempById(@PathVariable("id") Long id){
        TbTypeTemplate temp = templateService.findTempById(id);
        log.info(String.valueOf(temp));
        return temp;
    }

    @ApiOperation("添加模板")
    @PostMapping("/addTemp")
    public CommonResult addTemp(@RequestBody TbTypeTemplate template){
        try {
            templateService.addTemp(template);
            return new CommonResult(200, "添加成功！", template);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(400, "添加失败！");
        }
    }

    @ApiOperation("更新模板")
    @PostMapping("/updateTemp")
    public CommonResult updateTemp(@RequestBody TbTypeTemplate template){
        try {
            templateService.updateTemp(template);
            return new CommonResult(200, "修改成功！", template);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(400, "修改失败！");
        }
    }

    @ApiOperation("删除模板")
    @GetMapping("/delTemp/{id}")
    public CommonResult delTemp(@PathVariable("id") Long id){
        try {
            templateService.delTemp(id);
            return new CommonResult(200, "删除成功！", id);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(400, "删除失败！");
        }
    }

    @RequestMapping("/findBySpecList/{id}")
    public List<Map> findSpecList(@PathVariable("id") Long id){
        return templateService.findSpecList(id);
    }

}
